﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

//new
namespace Document_Management
{
    public partial class frmNC : Form
    {
        int width, height;
        static string connectionString = ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        int iflag = 0;
        int lag1 = 0;
        int lag2 = 0;
        string fileName;
        string ma = "";
        string ma1 = "";
        string ma2 = "";
        //static string connectionString ="Data Source=NGOCDAT\\SQLEXPRESS;Initial Catalog=庫存管理;Integrated Security=True";
        bool bNC程式Search, bNC程式Insert, bNC程式Edit, bNC程式Del;
        public frmNC(bool bNC程式Search, bool bNC程式Insert, bool bNC程式Edit, bool bNC程式Del)
        {
            InitializeComponent();
            width = this.Size.Width;
            height = this.Size.Height;
            this.bNC程式Search = bNC程式Search;
            this.bNC程式Insert = bNC程式Insert;
            this.bNC程式Edit = bNC程式Edit;
            this.bNC程式Del = bNC程式Del;
        }
        private void frmNC_Load(object sender, EventArgs e)
        {
            Load驗證人();
            LoadMain();
            Load程式撰寫人();
           //LoadTab2();

            ResetTextNC();
            ResetText驗證人();
            ResetText寫人();
        }

        
        public void LoadMain()
        {

            SqlConnection cnn = new SqlConnection(connectionString);
            try
            {
                cnn.Open();
               //string sql = "select * from NC程式";

                string sql = "SELECT NC程式.圖號,NC程式.夾治具,NC程式.對應加工技術文件,NC程式.工序名稱,NC程式.版次,NC程式.適用機台,NC程式.附件名稱,驗證人.驗證人代號,程式撰寫人.程式撰寫人代號 FROM NC程式 INNER JOIN 驗證人 ON NC程式.驗證人代號=驗證人.驗證人代號 INNER JOIN 程式撰寫人 ON NC程式.程式撰寫人代號=程式撰寫人.程式撰寫人代號";
                SqlDataAdapter ad = new SqlDataAdapter(sql, cnn);
                DataTable dt = new DataTable();
                ad.Fill(dt);
                dataGridHT.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGridHT.DataSource = dt;

                PublicClass.setRowNumber(dataGridHT);
                //dataGridHT.Columns[0].HeaderText = "asgas";
                dataGridHT.Columns[0].Width = 60;
                dataGridHT.Columns[1].Width = 70;

                cnn.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                cnn.Close();
            }
            //grpBoSung.Visible = false;
            this.Size = new System.Drawing.Size(this.Size.Width, height - grpBoSung.Height);
        }
        public void Load驗證人()
        {
           
            SqlConnection con = new SqlConnection(connectionString);
            try
            { 
                con.Open();
                string sql = "Select * From 驗證人";
                SqlDataAdapter ad = new SqlDataAdapter(sql, con);
                DataTable dt1 = new DataTable();
                ad.Fill(dt1);
                GV驗證人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                GV驗證人.DataSource = dt1;
               PublicClass.setRowNumber(GV驗證人);

               // int i = e.RowIndex;
               // GV驗證人.Rows[i].HeaderCell.Value = (i + 1).ToString();

                               con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }

            //this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height);
            this.Size = new System.Drawing.Size(this.Size.Width, height - grpBoSung.Height);
      
        }

        public void Load程式撰寫人()
        {
            SqlConnection con = new SqlConnection(connectionString);
            string sql = "Select * From 程式撰寫人";
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            GV寫人.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            GV寫人.DataSource = dt;
            PublicClass.setRowNumber(GV寫人);
            //
            this.Size = new System.Drawing.Size(this.Size.Width, height - grpBoSung.Height);
         
        }
        public void hide()
        {
            this.Size = new Size(width, height - grpBoSung.Height);
            grpBoSung.Visible = false;
            btThem.Enabled = true;
            btndelete.Enabled = true;
            btSua.Enabled = true;
        }
        private void bt1_Click(object sender, EventArgs e)
        {
            Enabled_Disibled_delete();
            hide();
            if (!bNC程式Search)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                string sp = "sp_NC_尋找";
                SqlCommand cmd = new SqlCommand(sp, con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (txt圖號.Text != "") cmd.Parameters.AddWithValue("@圖號", txt圖號.Text);
                if (txt版次.Text!= "") cmd.Parameters.AddWithValue("@版次", txt版次.Text);
                if (txt工序名稱.Text != "") cmd.Parameters.AddWithValue("@工序名稱", txt工序名稱.Text);
                if (txt適用機台.Text != "") cmd.Parameters.AddWithValue("@適用機台", txt適用機台.Text);
                if (txt夾治具.Text != "") cmd.Parameters.AddWithValue("@夾治具", txt夾治具.Text);
                if (txt對應加工.Text != "") cmd.Parameters.AddWithValue("@對應加工技術文件", txt對應加工.Text);
                if (txt附件.Text != "") cmd.Parameters.AddWithValue("@附件名稱", txt附件.Text);
                if (txt撰寫人.Text!= "") cmd.Parameters.AddWithValue("@程式撰寫人代號", txt撰寫人.Text);
                if (txt驗證人.Text != "") cmd.Parameters.AddWithValue("@驗證人代號", txt驗證人.Text);
                SqlDataReader sqlreader = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("圖號");
                dt.Columns.Add("版次");
                dt.Columns.Add("工序名稱");
                dt.Columns.Add("適用機台");
                dt.Columns.Add("夾治具");
                dt.Columns.Add("對應加工技術文件");
                dt.Columns.Add("附件名稱");
                dt.Columns.Add("程式撰寫人代號");
                dt.Columns.Add("驗證人代號");
               
                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["圖號"] = sqlreader["圖號"];
                    dtrows["版次"] = sqlreader["版次"];
                    dtrows["工序名稱"] = sqlreader["工序名稱"];
                    dtrows["適用機台"] = sqlreader["適用機台"];
                    dtrows["夾治具"] = sqlreader["夾治具"];
                    dtrows["對應加工技術文件"] = sqlreader["對應加工技術文件"];
                    dtrows["附件名稱"] = sqlreader["附件名稱"];
                    dtrows["程式撰寫人代號"] = sqlreader["程式撰寫人代號"];
                    dtrows["驗證人代號"] = sqlreader["驗證人代號"];
                    dt.Rows.Add(dtrows);

                }

                dataGridHT.DataSource = dt;
                PublicClass.setRowNumber(dataGridHT);
                //ResetTextNC();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }


        private void btSua_Click(object sender, EventArgs e)
        {
            if (!bNC程式Edit)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            iflag = 2;
            btninsertupdatedelete.Text = "更新";
            this.Size = new System.Drawing.Size(width, height);
            ResetTextNC();
            grpBoSung.Visible = true;
            Enabled_Disibled_Edit();
        
        }
        private void Enabled_Disibled_Edit()
        {
            txt圖號1.Enabled = true;
            txt適用機台1.Enabled = true;
            txt對應加工1.Enabled = true;
            txt附件名稱1.Enabled = true;
            txt夾治具1.Enabled = true;
            txt驗證人1.Enabled = true;
            txt工序名稱1.Enabled = true;
            txt撰寫人1.Enabled = true;
            txt版次1.Enabled = true;
        }
        private void btThem_Click(object sender, EventArgs e)
        {
            iflag = 1;
            btninsertupdatedelete.Text = "實現";
            //this.Size = new System.Drawing.Size(this.Size.Width, this.Size.Height+grpBoSung.Height);
            this.Size = new System.Drawing.Size(width, height);
            if (!bNC程式Insert)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ResetTextNC();
            grpBoSung.Visible = true;
            //An_allnut_them();
            Enabled_Disibled_Addnews();
        }
        private void Enabled_Disibled_Addnews()
        {
            btSua.Enabled = true;
            btThem.Enabled = true;
            btndelete.Enabled = true;
            txt圖號1.Enabled =true;
            txt適用機台1.Enabled = true;
            txt對應加工1.Enabled = true;
            txt附件名稱1.Enabled = true;
            txt夾治具1.Enabled = true;
            txt驗證人1.Enabled = true;
            txt工序名稱1.Enabled = true;
            txt撰寫人1.Enabled = true;
            txt版次1.Enabled = true;
        }

        public void ResetTextNC()
        {
            txt圖號.ResetText();
            txt版次.ResetText();
            txt工序名稱.ResetText();
            txt適用機台.ResetText();
            txt夾治具.ResetText();
            txt對應加工.ResetText();
            txt附件.ResetText();
            txt撰寫人.ResetText();
            txt驗證人.ResetText();

            txt圖號1.Text="";
            txt適用機台1.Text="";
            txt對應加工1.Text="";
            txt附件名稱1.Text="";
            txt夾治具1.Text="";
            txt驗證人1.Text="";
            txt工序名稱1.Text="";
            txt撰寫人1.Text="";
            txt版次1.Text="";

        }
        public void ResetText驗證人()
        {
            txt驗證人代號.ResetText();
            txt驗證人姓名.ResetText();
        }
        public void ResetText寫人()
        {
            txt程式撰寫人代號.ResetText();
            txt程式撰寫人姓名.ResetText();
       }
        private void An_allnut_sua()
        {
            txt圖號1.Enabled = false;
            txt適用機台1.Enabled = true;
            txt對應加工1.Enabled = true;
            txt附件名稱1.Enabled = true;
            txt夾治具1.Enabled = true;
            txt驗證人1.Enabled = true;
            txt工序名稱1.Enabled = true;
            txt撰寫人1.Enabled = false;
            txt版次1.Enabled = false;
            
        }
        private void An_allnut_xoa()
        {
            txt圖號1.Enabled = false;
            txt適用機台1.Enabled = false;
            txt對應加工1.Enabled = false;
            txt附件名稱1.Enabled = false;
            txt夾治具1.Enabled = false;
            txt驗證人1.Enabled = false;
            txt工序名稱1.Enabled = false;
            txt撰寫人1.Enabled = false;
            txt版次1.Enabled = false;
            bt_程式撰寫人代號_查詢.Enabled = false;
            bt_驗證人_查詢.Enabled = false;
           // bt_圖號_查詢.Enabled = true;
        }
        private void An_allnut_them()
        {
            btndelete.Enabled = true;
            btSua.Enabled = true;
            btThem.Enabled = true;
            txt圖號1.Enabled = true; //1
            txt適用機台1.Enabled = true; //2
            txt對應加工1.Enabled = true;//3
            txt附件名稱1.Enabled = true;//4
            txt夾治具1.Enabled = true;//5
            txt驗證人1.Enabled = true;//6
            txt工序名稱1.Enabled = true;//7
            txt撰寫人1.Enabled = false;//8
            txt版次1.Enabled = false;//9
         //   bt_圖號_查詢.Enabled = false;
            bt_程式撰寫人代號_查詢.Enabled = true;
            bt_驗證人_查詢.Enabled = true;
        }
        private void btndelete_Click(object sender, EventArgs e)
        {
            if (!bNC程式Del)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            iflag = 3;
            btninsertupdatedelete.Text = " 實現";
            this.Size = new System.Drawing.Size(width, height);
            grpBoSung.Visible = true;
            Enabled_Disibled_delete();
        }
        private void Enabled_Disibled_delete()
        {
            txt圖號1.Enabled = false;
            txt適用機台1.Enabled =false;
            txt對應加工1.Enabled = false;
            txt附件名稱1.Enabled = false;
            txt夾治具1.Enabled = false;
            txt驗證人1.Enabled = false;
            txt工序名稱1.Enabled = false;
            txt撰寫人1.Enabled = false;
            txt版次1.Enabled = false;
        }

        
        private void bt_圖號_查詢_Click(object sender, EventArgs e)
        {
            Search圖號 frm = new Search圖號(txt圖號1);
            frm.ShowDialog();
        }

        private void bt_圖號_查詢_Click_1(object sender, EventArgs e)
        {
            Search圖號 frm = new Search圖號(txt圖號1);
            frm.ShowDialog();
        }

        private void btnAddnew_Click(object sender, EventArgs e)
        {
            if (!bNC程式Insert)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SqlConnection con = new SqlConnection(connectionString);
                try
                {
                    if (txt驗證人代號.Text == "")
                    {
                        MessageBox.Show("請您輸入驗證人代號", "錯誤", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        txt驗證人代號.Focus();
                        return;
                    }

                    if (txt驗證人姓名.Text == "")
                    {
                        MessageBox.Show("請您輸入驗證人姓名", "錯誤", MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        txt驗證人姓名.Focus();
                        return;
                    }
                    con.Open();
                    string sp_insert = "sp_NC驗證人_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@驗證人代號", txt驗證人代號.Text);
                    cmd.Parameters.AddWithValue("@驗證人姓名", txt驗證人姓名.Text);
                    cmd.ExecuteNonQuery();
                   // MessageBox.Show("新增成功");
                    MessageBox.Show("您已經新增成功" + "\n" + "驗證人代號 :" + txt驗證人代號.Text + "\n" + "驗證人姓名 :" + txt驗證人姓名.Text, "警報", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Load驗證人();
                    ResetText驗證人();
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Close();
                    return;
                }
            //lag1 = 1;
            //btOK.Text = "實現";
            //ResetText驗證人();
            //txt驗證人代號.Enabled = true;
            //txt驗證人姓名.Enabled = true;

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (!bNC程式Edit)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (txt驗證人代號.Text == "") { MessageBox.Show("請您輸入驗證人代號更新", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                {

                    string sp_update = "sp_NC驗證人_更新";
                    SqlCommand cmd3 = new SqlCommand(sp_update, con);
                    cmd3.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    cmd3.Parameters.AddWithValue("@驗證人代號", txt驗證人代號.Text);
                    cmd3.Parameters.AddWithValue("@驗證人姓名", txt驗證人姓名.Text);
                    cmd3.Parameters.AddWithValue("@驗證人代號1",ma);
                    cmd3.ExecuteNonQuery();
                    MessageBox.Show("更新成功");
                    Load驗證人();
                    ResetText驗證人();
                    con.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
                return;
            }
            //lag1 = 2;
            //btOK.Text = "實現";
            //ResetText驗證人();
            //txt驗證人代號.Enabled = false;
            //txt驗證人姓名.Enabled = true;
            //btSelect_驗證人.Enabled = true;

        }

        private void btnDelete廠區_Click(object sender, EventArgs e)
        {
           
            if (!bNC程式Del)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
               
                if (txt驗證人代號.Text == "") { MessageBox.Show("請您輸入驗證人代號刪除", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (answer == DialogResult.Yes)
                    {
                        string sp_delete = "sp_NC驗證人_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@驗證人代號", txt驗證人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();

                        Load驗證人();
                        ResetText驗證人();
                        return;
                    }
                    else
                    {
                        Load驗證人();
                        con.Close();
                    }
                   
                }
            }
            catch (Exception ex)
            {
               
                MessageBox.Show(ex.Message);
                return;
            }
            //lag1 = 3;
            //btOK.Text = "實現";
            //ResetText驗證人();
            //txt驗證人代號.Enabled = false;
            //txt驗證人姓名.Enabled = false;
            //btSelect_驗證人.Enabled = true;
        }

        private void btAdd寫人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (!bNC程式Insert)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (txt程式撰寫人代號.Text == "")
                {
                    MessageBox.Show("請您輸入程式撰寫人代號", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt程式撰寫人代號.Focus();
                    return;
                }

                if (txt程式撰寫人姓名.Text == "")
                {
                    MessageBox.Show("請您輸入程式撰寫人姓名", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txt程式撰寫人姓名.Focus();
                    return;
                }
                con.Open();
                string sp_insert = "sp_NC程式撰寫人_新增";
                SqlCommand cmd = new SqlCommand(sp_insert, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@程式撰寫人代號", txt程式撰寫人代號.Text);
                cmd.Parameters.AddWithValue("@程式撰寫人姓名", txt程式撰寫人姓名.Text);
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增成功");
                Load程式撰寫人();
                ResetText寫人();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
                return;
            }
            //lag2 = 1;
            //btOk_寫人.Text = "實現";
            //ResetText寫人();
            //txt程式撰寫人代號.Enabled = true;
            //txt程式撰寫人姓名.Enabled = true;
        }

        private void btEdit寫人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (!bNC程式Edit)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (txt程式撰寫人代號.Text == "") { MessageBox.Show("請您輸入程式撰寫人代號", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                {

                    string sp_update = "sp_NC程式撰寫人_更新";
                    SqlCommand cmd3 = new SqlCommand(sp_update, con);
                    cmd3.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    cmd3.Parameters.AddWithValue("@程式撰寫人代號", txt程式撰寫人代號.Text);
                    cmd3.Parameters.AddWithValue("@程式撰寫人姓名", txt程式撰寫人姓名.Text);
                    cmd3.Parameters.AddWithValue("@程式撰寫人代號1", ma2);
                    cmd3.ExecuteNonQuery();
                    MessageBox.Show("更新成功");
                    Load程式撰寫人();
                    ResetText寫人();
                    con.Close();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Close();
                return;
            }   
            //lag2 = 2;
            //btOk_寫人.Text = "實現";
            //ResetText寫人();
            //txt程式撰寫人代號.Enabled = false;
            //txt程式撰寫人姓名.Enabled = true;
            ////btSelect_驗證人.Enabled = true;
            //btSearch寫人.Enabled = true;
        }

        private void btDelete寫人_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (!bNC程式Del)
            {
                MessageBox.Show("很抱歉，您没有权限", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (txt程式撰寫人代號.Text == "") { MessageBox.Show("請您輸入程式撰寫人代號刪除", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                else
                {
                    con.Open();
                    DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (answer == DialogResult.Yes)
                    {
                        string sp_delete = "sp_NC程式撰寫人_刪除";
                        SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                        sqldelete.CommandType = CommandType.StoredProcedure;
                        sqldelete.Parameters.AddWithValue("@程式撰寫人代號", txt程式撰寫人代號.Text);
                        MessageBox.Show("刪除成功");
                        sqldelete.ExecuteNonQuery();
                        Load程式撰寫人();
                        ResetText寫人();
                        return;
                    }
                    else
                    {
                        Load程式撰寫人();
                        return;
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.ToString());
                return;
            }
            //lag2 = 3;
            //btOk_寫人.Text = "實現";
            //ResetText寫人();
            //txt程式撰寫人代號.Enabled = false;
            //txt程式撰寫人姓名.Enabled = false;
            //btSearch寫人.Enabled = true;
        }
        
        ////private void button1_Click(object sender, EventArgs e)
        ////{
        ////    Search驗證人 frm2 = new Search驗證人(txt驗證人代號);
        ////    frm2.ShowDialog();
        ////}

        /////
        ////private void btSearch寫人_Click(object sender, EventArgs e)
        ////{
        ////    Search程式撰寫人 frm1 = new Search程式撰寫人(txt程式撰寫人代號);
        ////    frm1.ShowDialog();
        ////}

        //private void dataGridHT_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    Load程式撰寫人();
        //    Load驗證人();
        //}

        private void dataGridHT_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridHT);
        }

        private void dataGridHT_RowHeaderMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(dataGridHT);
        }

        private void GV驗證人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(GV驗證人);
        }

        private void GV驗證人_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(GV驗證人);
        }

        private void GV驗證人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int rt=e.RowIndex;
                txt驗證人代號.Text = GV驗證人.Rows[rt].Cells[0].Value.ToString();
                 ma = GV驗證人.Rows[rt].Cells[0].Value.ToString();
                txt驗證人姓名.Text = GV驗證人.Rows[rt].Cells[1].Value.ToString();
            }
            catch
            {
 
            }
        }

        private void dataGridHT_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int rt1 = e.RowIndex;
                ma1= dataGridHT.Rows[rt1].Cells[0].Value.ToString();
                txt圖號1.Text = dataGridHT.Rows[rt1].Cells[0].Value.ToString();
                txt夾治具1.Text = dataGridHT.Rows[rt1].Cells[1].Value.ToString();
                txt對應加工1.Text = dataGridHT.Rows[rt1].Cells[2].Value.ToString();
                txt工序名稱1.Text = dataGridHT.Rows[rt1].Cells[3].Value.ToString();
                txt版次1.Text = dataGridHT.Rows[rt1].Cells[4].Value.ToString();
                txt適用機台1.Text = dataGridHT.Rows[rt1].Cells[5].Value.ToString();
                txt附件名稱1.Text = dataGridHT.Rows[rt1].Cells[6].Value.ToString();
                txt驗證人1.Text = dataGridHT.Rows[rt1].Cells[7].Value.ToString();
                txt撰寫人1.Text = dataGridHT.Rows[rt1].Cells[8].Value.ToString();
            }
            catch
            { 
            }
        }

        private void GV寫人_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int rt = e.RowIndex;
                txt程式撰寫人代號.Text = GV寫人.Rows[rt].Cells[0].Value.ToString();
                txt程式撰寫人姓名.Text = GV寫人.Rows[rt].Cells[1].Value.ToString();
                ma2 = GV寫人.Rows[rt].Cells[0].Value.ToString();
            }
            catch
            { 
            }
        }

        private void GV寫人_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(GV寫人);
        }

        private void GV寫人_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PublicClass.setRowNumber(GV寫人);
        }

        private void tab2_SelectedIndexChanged(object sender, EventArgs e)
        {
            grpBoSung.Visible = false;
            this.Size = new Size(width, height - grpBoSung.Height);
            PublicClass.setRowNumber(dataGridHT);
            PublicClass.setRowNumber(GV寫人);
            PublicClass.setRowNumber(GV驗證人);
            ResetTextNC();
            ResetText寫人();
            ResetText驗證人();
            //grpBoSung.Visible = true;
        }

        private void bt_驗證人_查詢_Click(object sender, EventArgs e)
        {
            Search驗證人 frm2 = new Search驗證人(txt驗證人1);
            frm2.ShowDialog();
        }

        private void bt_程式撰寫人代號_查詢_Click(object sender, EventArgs e)
        {
            Search程式撰寫人 frm1 = new Search程式撰寫人(txt撰寫人1);
            frm1.ShowDialog();
        }

        private void txt驗證人1_TextChanged(object sender, EventArgs e)
        {           
        }

        private void txt撰寫人1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 程式撰寫人 where 程式撰寫人代號=N'{0}'", txt撰寫人1.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbt撰寫人1.Text = "";
                else lbt撰寫人1.Text = temp;
            }
            catch (Exception ex) { }
        }

        private void btninsertupdatedelete_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            if (iflag == 1)//insert
            {
                try
                {
                    //1
                    if (txt圖號1.Text == "")
                    {
                        MessageBox.Show("請您輸入圖號", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt圖號1.Focus();
                        return;
                    }
                    //2
                    if (txt適用機台1.Text == "")
                    {
                        MessageBox.Show("請您輸入版次", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt適用機台1.Focus();
                        return;
                    }
                    //3
                    if (txt對應加工1.Text == "")
                    {
                        MessageBox.Show("請您輸入工序名稱", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt對應加工1.Focus();
                        return;
                    }
                    //4
                    if (txt附件名稱1.Text == "")
                    {
                        MessageBox.Show("請您輸入適用機台", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt附件名稱1.Focus();
                        return;
                    }
                    //5
                    if (txt夾治具1.Text == "")
                    {
                        MessageBox.Show("請您輸入夾治具", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt夾治具1.Focus();
                        return;
                    }
                    //6
                    if (txt驗證人1.Text == "")
                    {
                        MessageBox.Show("請您輸入對應加工", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt驗證人1.Focus();
                        return;
                    }
                    //7
                    if (txt工序名稱1.Text == "")
                    {
                        MessageBox.Show("請您輸入附件", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt工序名稱1.Focus();
                        return;
                    }
                    //8
                    if (txt撰寫人1.Text == "")
                    {
                        MessageBox.Show("請您輸入撰寫人", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt撰寫人1.Focus();
                        return;
                    }

                    //9
                    if (txt版次1.Text == "")
                    {
                        MessageBox.Show("請您輸入驗證人", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txt版次1.Focus();
                        return;
                    }
                    con.Open();
                    string sp_insert = "sp_NC_新增";
                    SqlCommand cmd = new SqlCommand(sp_insert, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@圖號", txt圖號1.Text);
                    cmd.Parameters.AddWithValue("@夾治具", txt夾治具1.Text);
                    cmd.Parameters.AddWithValue("@工序名稱", txt工序名稱1.Text);
                    cmd.Parameters.AddWithValue("@附件名稱", fileName);
                    cmd.Parameters.AddWithValue("@驗證人代號", txt驗證人1.Text);
                    cmd.Parameters.AddWithValue("@版次", txt版次1.Text);
                    cmd.Parameters.AddWithValue("@適用機台", txt適用機台1.Text);
                    cmd.Parameters.AddWithValue("@對應加工技術文件", txt對應加工1.Text);
                    cmd.Parameters.AddWithValue("@程式撰寫人代號", txt撰寫人1.Text);
          
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("新增成功");
                    LoadMain();
                    this.Size = new System.Drawing.Size(width, height);
                    grpBoSung.Visible = true;
                    ResetTextNC();
                    hide();
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
            if (iflag == 2)//cap nhat
            {
                try
                {                   
                        string sp_update = "sp_NC_更新";
                        SqlCommand cmd2 = new SqlCommand(sp_update, con);
                        cmd2.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        cmd2.Parameters.AddWithValue("@圖號", txt圖號1.Text);
                        cmd2.Parameters.AddWithValue("@夾治具", txt夾治具1.Text);
                        cmd2.Parameters.AddWithValue("@工序名稱", txt工序名稱1.Text);
                        cmd2.Parameters.AddWithValue("@附件名稱", txt附件名稱1.Text);
                        cmd2.Parameters.AddWithValue("@驗證人代號", txt驗證人1.Text);
                        cmd2.Parameters.AddWithValue("@版次", txt版次1.Text);
                        cmd2.Parameters.AddWithValue("@適用機台", txt適用機台1.Text);
                        cmd2.Parameters.AddWithValue("@對應加工技術文件", txt對應加工1.Text);
                        cmd2.Parameters.AddWithValue("@程式撰寫人代號", txt撰寫人1.Text);
                        cmd2.Parameters.AddWithValue("@圖號1", ma1);
                        cmd2.ExecuteNonQuery();
                        MessageBox.Show("更新成功", "通訊", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        LoadMain();
                        this.Size = new System.Drawing.Size(width, height);
                        grpBoSung.Visible = true;
                        con.Close();
                        hide();
                        return;                
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    LoadMain();
                    Close();
                    return;
                }
            }

            if (iflag == 3)//delete
            {
                try
                {
                    if (txt圖號1.Text == "") { MessageBox.Show("請您輸入圖號刪除", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                    else
                    {
                        con.Open();
                        DialogResult answer = MessageBox.Show("您確定要出入這個產品碼", "警報", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (answer == DialogResult.Yes)
                        {
                            string sp_delete = "sp_NC_刪除";
                            SqlCommand sqldelete = new SqlCommand(sp_delete, con);
                            sqldelete.CommandType = CommandType.StoredProcedure;
                            sqldelete.Parameters.AddWithValue("@圖號", txt圖號1.Text);
                            MessageBox.Show("刪除成功");
                            sqldelete.ExecuteNonQuery();
                            LoadMain();
                            grpBoSung.Visible = true;
                            hide();
                            return;
                        }
                        else
                        {
                            LoadMain();
                            return;
                        }
                        con.Close();
                    }
                }
                catch (Exception ex)
                {
                    con.Close();
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            grpBoSung.Visible = false;
            this.Size = new Size(width, height - grpBoSung.Height);
            btThem.Enabled = true;
            btndelete.Enabled = true;
            btSua.Enabled = true;
        }

        private void txt對應加工1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 驗證人 where 驗證人代號=N'{0}'", txt驗證人1.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lbt驗證人1.Text = "";
                else lbt驗證人1.Text = temp;
            }
            catch (Exception ex) { }
        }

        private void dataGridHT_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void bt_匯出_Click(object sender, EventArgs e)
        {
            Export_Excel excel = new Export_Excel();
            DataTable dt = (DataTable)dataGridHT.DataSource;
            excel.Export(dt, "Sheet1", "NC資訊");
        }

        private void grp驗證人_Enter(object sender, EventArgs e)
        {

        }

        private void bt_重置_Click(object sender, EventArgs e)
        {
            txt圖號.Text = "";
            txt版次.Text = "";
            txt工序名稱.Text = "";
            txt適用機台.Text = "";
            txt夾治具.Text = "";
            txt對應加工.Text = "";
            txt附件.Text = "";
            txt撰寫人.Text = "";
            txt驗證人.Text = "";
            LoadMain();
        }

        private void txt附件名稱1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnchose_Click(object sender, EventArgs e)
        {            
            string path = "";
            string Rrl = "";
            string Rrl2 = "";
            string temp = "";
            string temp2 = "";
            string fileToCopy = "";
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "開啟檔案(Attach customer proposal document)";
            //fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
            fDialog.Filter = "檔案當 (*.txt, *.doc, *.docx, *.xls, *.xlsx, *.ppt, *.pptx, *.pdf) | *.txt; *.doc; *.docx; *.xlsx;*.xls; *.ppt; *.pptx; *.pdf | All Files|*.*";
            fDialog.InitialDirectory = Application.StartupPath + @"Documents\\";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = System.IO.Path.GetFileName(fDialog.FileName);
                path = Path.GetDirectoryName(fDialog.FileName);
                txt附件名稱1.Text = path + "\\" + fileName;
                //copyfile(); 
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml("C:\\config\\configNC.xml");
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        temp += dr["UrlServer"].ToString() + "\n";
                        temp2 += dr["UrlClient"].ToString() + "\n";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                    temp2 = temp2.Substring(0, temp2.Length - 1);
                    Rrl = temp;
                    Rrl2 = temp2;
                    //==================================================
                    //string destinationDirectory = @"C:\workspace\";
                    string destinationDirectory = @"" + Rrl + "";
                    fileToCopy = txt附件名稱1.Text;
                    File.Copy(fileToCopy, destinationDirectory + Path.GetFileName(fileToCopy), true);
                    txt附件名稱1.Text = destinationDirectory + "\\" + fileName;
                }
                catch { }
            }
        }

        private void dataGridHT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string Rrl = "";
            string Rrl2 = "";
            string temp = "";
            //string temp2 = "";
            try
            {

                //OpenFileDialog fDialog = new OpenFileDialog();
                //fDialog.Title = "開啟檔案(Attach customer proposal document)";

                DialogResult result = new DialogResult();
                PublicClass.setRowNumber(dataGridHT);
                if (dataGridHT.CurrentCell.ColumnIndex.Equals(6))
                {
                    if (dataGridHT.CurrentCell != null && dataGridHT.CurrentCell.Value != null)
                    {
                        string rooturl = dataGridHT.CurrentCell.Value.ToString();
                        result = MessageBox.Show("你要不要存檔這個資料？" + "\n" + rooturl, "存檔NC", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            FolderBrowserDialog fDialogSave = new FolderBrowserDialog();
                            if (fDialogSave.ShowDialog() == DialogResult.OK)
                            {
                                string foldername = fDialogSave.SelectedPath;
                                DataSet ds = new DataSet();
                                ds.ReadXml("C:\\config\\configNC.xml");
                                foreach (DataRow dr in ds.Tables[0].Rows)
                                {
                                    temp += dr["UrlServer"].ToString() + "\n";
                                    //temp2 += dr["UrlClient"].ToString() + "\n";
                                }
                                temp = temp.Substring(0, temp.Length - 1);
                                //temp2 = temp2.Substring(0, temp2.Length - 1);
                                Rrl = temp;
                                Rrl = Rrl + Path.GetFileName(dataGridHT.CurrentCell.Value.ToString());
                                //Rrl2 = temp2;
                                Rrl2 = foldername + "\\" + Path.GetFileName(dataGridHT.CurrentCell.Value.ToString());
                                File.Copy(Rrl, Rrl2, true);
                                MessageBox.Show("另存為成功");
                                //LoadMain();
                                return;
                            }
                        }
                        if (result == DialogResult.No)
                        {
                            //MessageBox.Show("還沒成功");
                            LoadMain();
                            return;
                        }
                    }
                }
            }
            catch { }
        }
    }
}

    

